package com.uniqueyi.baselib.utilsbase.file;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.uniqueyi.baselib.utilsbase.log.LogUtil;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by
 * author:  cwy.
 * date:    2018/1/9.  14:42.
 * prj:     crxls.
 * <p>
 * 创建文件夹
 */

public class FileUtils {
    private static final String TAG = "FileUtils";

    public static final int FLAG_SUCCESS = 1;//创建成功
    public static final int FLAG_EXISTS = 2;//已存在
    public static final int FLAG_FAILED = 3;//创建失败
    public static final int FLAG_FAILED_EXP = 4;//创建异常


    /**
     * 创建 单个 文件
     *
     * @param filePath 待创建的文件路径
     * @return 结果码
     */
    public static int CreateFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            LogUtil.getLogUtil().log(TAG, "The file [ " + filePath + " ] has already exists");
            return FLAG_EXISTS;
        }
        if (filePath.endsWith(File.separator)) {// 以 路径分隔符 结束，说明是文件夹
            LogUtil.getLogUtil().log(TAG, "The file [ " + filePath + " ] can not be a directory");
            return FLAG_FAILED;
        }

        //判断父目录是否存在
        if (!file.getParentFile().exists()) {
            //父目录不存在 创建父目录
            LogUtil.getLogUtil().log(TAG, "creating parent directory...");
            if (!file.getParentFile().mkdirs()) {
                LogUtil.getLogUtil().log(TAG, "created parent directory failed.");
                return FLAG_FAILED;
            }
        }

        //创建目标文件
        try {
            if (file.createNewFile()) {//创建文件成功
                LogUtil.getLogUtil().log(TAG, "create file [ " + filePath + " ] success");
                return FLAG_SUCCESS;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.getLogUtil().log(TAG, "create file [ " + filePath + " ] failed");
            return FLAG_FAILED;
        }

        return FLAG_FAILED;
    }

    /**
     * 创建 文件夹
     *
     * @param dirPath 文件夹路径
     * @return 结果码
     */
    public static int createDir(String dirPath) {
        File dir = new File(dirPath);
        //文件夹是否已经存在
        if (dir.exists()) {
            LogUtil.getLogUtil().log(TAG, "The directory [ " + dirPath + " ] has already exists");
            return FLAG_EXISTS;
        }
        if (!dirPath.endsWith(File.separator)) {//不是以 路径分隔符 "/" 结束，则添加路径分隔符 "/"
            dirPath = dirPath + File.separator;
        }
        //创建文件夹
        if (dir.mkdirs()) {
            LogUtil.getLogUtil().log(TAG, "create directory [ " + dirPath + " ] success");
            return FLAG_SUCCESS;
        }

        LogUtil.getLogUtil().log(TAG, "create directory [ " + dirPath + " ] failed");
        return FLAG_FAILED;
    }


    public static byte[] fileToByte(File file) throws IOException {
        FileInputStream input = new FileInputStream(file);
        byte[] byt = inputStreamToByte(input);
        return byt;
    }


    public static byte[] inputStreamToByte(FileInputStream input) throws IOException {
        byte[] ret = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
            byte[] b = new byte[4096];
            int n;
            while ((n = input.read(b)) != -1) {
                out.write(b, 0, n);
            }
            input.close();
            out.close();
            ret = out.toByteArray();
        } catch (IOException e) {
            // log.error("helper:get bytes from file process error!");
            e.printStackTrace();
        }
        return ret;
    }

    public static InputStream byteToInputStream(byte[] bytes) {
        return new ByteArrayInputStream(bytes);
    }

    public static File byteToFile(String pathName,byte[] bytes) throws IOException {
        File file = new File(pathName);
        OutputStream output = new FileOutputStream(file);
        BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
        bufferedOutput.write(bytes);
        return file;
    }

    public static Bitmap fileToBitmap(File file) {
        Bitmap bm = BitmapFactory.decodeFile(file
                .getAbsolutePath());
        return bm;
    }

    /** 是否有SD卡 */
    public static boolean isHaveSDCard() {
        String SDState = android.os.Environment.getExternalStorageState();
        if (SDState.equals(android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }
}
